package org.example.SolutionNew001;

import java.util.Arrays;

public class Matrix03 {
    public static void main(String[] args) {
        Matrix03 m = new Matrix03();
        int[][] matrix=new int[][]{{1,2,3},{4,5,6},{7,8,9}};
        m.rotate(matrix);
        for(int i=0; i<matrix.length; i++){
            System.out.println(Arrays.toString(matrix[i]));
        }
    }

    public void rotate1(int[][] matrix) {
        int m = matrix.length;
        for(int i=0;i<m/2;i++){
            for(int j=0;j<(m+1)/2;j++){
                int temp = matrix[i][j];
                matrix[i][j] = matrix[m-j-1][i];
                matrix[m-j-1][i] = matrix[m-i-1][m-j-1];
                matrix[m-i-1][m-j-1] = matrix[j][m-i-1];
                matrix[j][m-i-1] = temp;
            }
        }
    }

    //一切算法的基础是等式   核心等式：matrix[row][col]=matrix[col][n-row-1]
    public void rotate(int[][] matrix) {
        int m = matrix.length;
        //水平翻转
        for(int i=0;i<m/2;i++){
            for(int j=0;j<m;j++){
                int temp = matrix[i][j];
                matrix[i][j] = matrix[m-i-1][j];
                matrix[m-i-1][j] = temp;
            }
        }
        //主对角线翻转
        for(int i=0;i<m;i++){
            for(int j=0;j<i;j++){
                int temp=matrix[i][j];
                matrix[i][j]=matrix[j][i];
                matrix[j][i]=temp;
            }
        }
    }
}
